package com.xtwy.service.impl;

import com.xtwy.entity.Menu;
import com.xtwy.repository.MenuRepository;
import com.xtwy.repository.RoleRepository;
import com.xtwy.service.MenuService;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @作 者: 侯建军
 * @日 期: 2018/7/29 14:03
 * @描 述:
 * @公 司：山西迅腾伟业
 */
@Log
@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    MenuRepository menuRepository;
    @Resource
    RoleRepository roleRepository;

    @Override
    public List<Menu> getMenus(String username) {
        List<Menu> menus = new ArrayList<>();
        List<Integer> ids = roleRepository.findByUsername(username);
        Set<Map> set = menuRepository.findByRoleIds(ids);
        for (Map map : set) {
            Menu menu = new Menu();
            menu.setName(map.get("mname").toString());
            menu.setUrl(map.get("url").toString());
            menus.add(menu);
        }
        return menus;
    }

    @Override
    public List<Menu> getMenus(Long roleId) {
        List<Menu> menus = new ArrayList<>();
        List<Map> maps = menuRepository.findByRoleId(roleId);
        log.warning(maps.size() + "");
        for (Map en : maps) {
            Menu menu = new Menu();
            menu.setUrl(en.get("url").toString());
            menus.add(menu);
        }
        return menus;
    }
}
